package com.linewell.commom.controller;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Query;

import com.linewell.commom.dao.impl.BaseDaoImpl;
import com.linewell.system.model.SysMenus;
import com.linewell.system.pagemodel.LoginInfo;

public abstract class BackendBaseController extends BaseDaoImpl {
	
	private static final Log log = LogFactory.getLog(BackendBaseController.class);
	/**
	 * 进入到列表页面
	 * @return
	 */
	protected String index(String view_list,HttpServletRequest request){
		
		log.info("------------------------------------------------以下为加载页面菜单功能---------------------------------------------------");
		StringBuffer buttons = new StringBuffer();  	
    	String url = request.getServletPath();	//当前访问路径
    	String hql = null;
		Query query = null;
		List<SysMenus> lstAll = null;
		SysMenus lst = null;
		try {
			hql = " from SysMenus t where t.url = '"+url+"' ";
			query = super.sessionFactory.getCurrentSession().createQuery(hql);
			lst = (SysMenus) query.list().get(0);
		} catch (HibernateException e) {
			System.out.println("异常" + e.getMessage());
			e.printStackTrace();
		}
		Integer id = lst.getId();
		try {
			hql = " from SysMenus t where t.status = 9 and  t.url != '' and t.parentId in ( "+id+") order by showOrder asc ";
			query = super.sessionFactory.getCurrentSession().createQuery(hql);
			lstAll = query.list();
		} catch (HibernateException e) {
			System.out.println("异常" + e.getMessage());
			e.printStackTrace();
		}
    	Iterator<SysMenus> it = lstAll.iterator();
    	while(it.hasNext()) {
    		SysMenus rd = (SysMenus)it.next();
    		buttons.append("<a href=\"javascript:void(0);\" class=\"easyui-linkbutton\" data-options=\"iconCls:'"+ rd.getImg() +"',plain:true\" onclick=\""+rd.getUrl()+"\" id=\"BUTTON"+ String.valueOf(rd.getId()) +"\">"+rd.getName()+"</a>");
    	}
		request.setAttribute("buttons", buttons.toString());
		log.info("------------------------------------------------页面菜单功能加载完成---------------------------------------------------");
		return view_list;
	}
	//********************************以下方法为获得当前登录用户基本信息*****************************************
	/**
     * 获取当前登录用户的ID号
     * @return
     */
    public static Integer getLoginUserId(HttpServletRequest request){
        HttpSession session = request.getSession();
        LoginInfo obj = (LoginInfo)session.getAttribute("user");
        return obj.getUserId();
    }
    /**
     * 获取当前登录用户的IP
     * @return
     */
    public static String getLoginUserIP(HttpServletRequest request) {
        HttpSession session = request.getSession();
        LoginInfo obj = (LoginInfo)session.getAttribute("user");
        return obj.getIp();
    }
    
    /**
     * 获取当前登录用户的名称
     * @return
     */
    public static String getLoginUserName(HttpServletRequest request) {
        HttpSession session = request.getSession();
        LoginInfo obj = (LoginInfo)session.getAttribute("user");
        return obj.getUserName();
    }
    
    /**
     * 获取当前登录用户的部门编号
     * @return
     */
    public static Integer getUserDepId(HttpServletRequest request) {
        HttpSession session = request.getSession();
        if (session.getAttribute("user") != null && session.getAttribute("user") != "") {
        	LoginInfo obj = (LoginInfo)session.getAttribute("user");
        	 return obj.getDepId();
        }
        return null;  
    }
  
    /**
     * 获取当前登录用户的角色编号
     * @return
     */  
    public static Integer getUserRoleId(HttpServletRequest request) {
        HttpSession session = request.getSession();
        LoginInfo obj = (LoginInfo)session.getAttribute("user");
        return obj.getRoleId();
    }
   
    /**
     * 获取当前登录用户的部门名称
     * @return
     */ 
    public static String getUserDepName(HttpServletRequest request) {
        HttpSession session = request.getSession();
        LoginInfo obj = (LoginInfo)session.getAttribute("user");
        return obj.getDepName();
    }
    
    /**
     * 获取当前登录用户的角色名称
     * @return
     */
    public static String getUserRoleName(HttpServletRequest request) {
        HttpSession session = request.getSession();
        LoginInfo obj = (LoginInfo)session.getAttribute("user");
        return obj.getRoleName();
    }
    
    /**
     * 获取当前登录用户的当前IP
     * @return
     */
    public static String getUserIp(HttpServletRequest request) {
        HttpSession session = request.getSession();
        LoginInfo obj = (LoginInfo)session.getAttribute("user");
        return obj.getIp();
    }
    
    /**
     * 获取当前登录用户可操作的菜单
     * @return
     */
    public static String getUserMoudleIds(HttpServletRequest request) {
    	StringBuffer sb = new StringBuffer();
        HttpSession session = request.getSession();
        LoginInfo obj = (LoginInfo)session.getAttribute("user");
        Set<String> ids = obj.getModuleIds();
        Iterator it = ids.iterator();
        while(it.hasNext())
        {
        	if(sb.length()>0)
        	{
        		sb.append(",");
        	}
        	sb.append(it.next().toString());
        }
        return sb.toString();
    }  	
}
